/**
 * 
 */
package lt.vin.exaatto.client.ui;

import lt.vin.exaatto.client.events.ElementSelectedEvent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Vincentas
 *
 */
public class ElementWidget<T> extends Composite {

	private static ElementWidgetUiBinder uiBinder = GWT.create(ElementWidgetUiBinder.class);

	interface ElementWidgetUiBinder extends UiBinder<Widget, ElementWidget> {
	}

	@UiField
	Label label;
	
	@UiField
	Anchor remove;
	
	private T content;
	
	private boolean selected = false;
	
	private BaseContainerWidget<T> parent;
	
	private boolean removable = true;
	
	public ElementWidget(T content, BaseContainerWidget<T> parent) {
		this(content, parent, true);
	}

	public ElementWidget(T content, BaseContainerWidget<T> parent, boolean removable) {
		this.parent = parent;
		this.content = content;
				
		initWidget(uiBinder.createAndBindUi(this));

		label.setText(content.toString());
		
		setRemovable(removable);
	}
	
	@UiHandler("remove")
	public void onRemove(ClickEvent event) {
		if (removable) {
			parent.onRemoveElement(ElementWidget.this.content);
		}
	}
	
	@UiHandler("label")
	public void onSelect(ClickEvent event) {
		Stat.getHandlerManager().fireEvent(new ElementSelectedEvent<T>(content));
	}

	/**
	 * @return the content
	 */
	public T getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(T content) {
		this.content = content;
	}

	/**
	 * @return the selected
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * @param selected the selected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	/**
	 * @return the removable
	 */
	public boolean isRemovable() {
		return removable;
	}

	/**
	 * @param removable the removable to set
	 */
	public void setRemovable(boolean removable) {
		this.removable = removable;
		
		remove.setVisible(removable);
	}
}
